<?php

namespace App\Console\Commands\Fee;

use App\Models\Fee\FeeAnnualOrderModel;
use App\Remote\Rms\RmsMqttRemote;
use App\Remote\Rms\RmsShopRemote;
use Carbon\Carbon;
use Illuminate\Console\Command;

class OrderOverdueClose extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fee:order-overdue-close';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '年费订单过期关闭脚本';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        //获得未支付已过期的订单
        FeeAnnualOrderModel::query()
            ->whereIn('payStatus', [FeeAnnualOrderModel::PAY_STATUS_WAIT, FeeAnnualOrderModel::PAY_STATUS_PART])
            ->each(function ($item) {
                //收银发送MQTT弹窗判断
                if (time() > $item->endTime) {
                    RmsMqttRemote::sendMqtt($item->shopId, RmsMqttRemote::MQTT_COMMAND_FEE_ORDER, ['day' => -1]);
                }

                // 8天后关闭收银
                if ($item->endTime < Carbon::now()->subDays(8)->timestamp) {
                    RmsShopRemote::updateShopFieldById($item->shopId, ['cashRegisterStatus' => RmsShopRemote::SHOP_CASH_REGISTER_STATUS_FEE_OFF]);
                }
            });
    }
}
